#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>

// gcc monitor.c -o monitor

int main(int argc, char const *argv[], char ** environ)
{
    printf("%s\n", environ[0]);
    int fd[2];
    pid_t pid;
    if (argc < 2) {
        printf("参数缺失！\n");
        exit(1);
    }
    if (pipe(fd)) {
        printf("管道创建失败\n");
        exit(1);
    }
    pid = fork();
    if (pid < 0) {
        perror("fork 失败\n");
        exit(1);
    }
    else if (pid == 0) {
        // 子进程
        close(0);
        dup(fd[0]);
        execve("ctrlprocess", (void *)argv, environ);
        exit(0);
    }else {
        close(fd[0]);
        write(fd[1], argv[1], strlen(argv[1]));
        write(fd[1], argv[1], strlen(argv[1]));
        wait(0);
    }
    return 0;
}
